<?php
namespace app\common\model\customer;
use app\common\model\user\User;
use app\api\controller\Pub;
use app\common\model\apiinput\Apiyunkefriends;
use app\common\model\apiinput\Apiyunkerecords; 
use think\Model;

class CustomerVisit extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'customervisit';
    protected $pk    = 'CustomerVisitId';
    //业务员的数据
    public function bussinfo()
    {
        return $this->hasOne('app\common\model\user\User', 'UserId', 'BussinessId');
    }
    //数企的数据
    public function shuqiinfo(){
        return $this->hasOne('app\common\model\apiinput\Apishuqicalllistpush', 'sessionId', 'CallsessionId');
    }
    //客户的数据
    public function customerinfo(){
        return $this->hasOne('app\common\model\customer\Customer', 'CustomerId', 'CustomerId');
    }


    /**
     * 获取回访记录
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function getlist($id)
    {
        $ndata = CustomerVisit::where('CustomerId', $id);
        $with = ['bussinfo'];
        if(get_config_value('call_center_switch')){
            $with[] = 'shuqiinfo';
        }
        $ndata = $ndata->order('VisitTime desc')->with($with)->hidden(['bussinfo.Jwt','bussinfo.WechatId','bussinfo.MpWechatId','bussinfo.Sex','bussinfo.Role','bussinfo.State']);

        $ndata = $ndata->select();

        //获取云客的微信聊天数据，在这个位置将数据整合到一起并进行排序
        if(get_config_value('yunke_switch')){
            $yunke_wechat_record = Apiyunkefriends::get_record($id);
            $ndata = array_merge($ndata->toArray(),$yunke_wechat_record);
            usort($ndata, function($a,$b){
                return strtotime($b['AddTime']) - strtotime($a['AddTime']);
            });
        }

        return $ndata;
    }

    /**
     * 增加回访记录
     * @param  [type] $CustomerId [description]
     * @param  [type] $goodlist   [description]
     * @param  [type] $valuelist  [description]
     * @param  [type] $pricelist  [为-1时则为业务员报价 否则是会计报价]
     * @param  array  $boxlist    [description]
     * @return [type]             [description]
     */
    public static function screate($CustomerId, $Content, $VisitTime = '', $Img = '')
    {
        if (empty($VisitTime)) {
            $VisitTime = date('Y-m-d H:i:s', time());
        }
        $cdata                = new CustomerVisit;
        $cdata['CustomerId']  = $CustomerId;
        $cdata['Content']     = $Content;
        $cdata['VisitTime']   = $VisitTime;
        $cdata['Img']         = $Img;

        $cdata['AddTime']     = date('Y-m-d H:i:s',time());
        $cdata['BussinessId'] = Pub::userid();
        $cdata->save($cdata);
        return sok();
    }

    /**
     * 获取统计数据
     * @return [type] [description]
     */
    public static function getsummary($date = '', $enddate = '')
    {
        if (empty($date)) {
            $date = date('Y-m-d', time());
        } else {
            $date = date('Y-m-d', strtotime($date));
        }
        if (empty($enddate)) {
            $list            = CustomerVisit::where('AddTime', '>', $date)->where('AddTime', '<=', $date . ' 24:00')->group('CustomerId')->field('BussinessId')->select();
            $rdata['timestr'] = mb_substr($date, 8, 2) . '号';
        } else {
            $enddate = date('Y-m-d', strtotime($enddate));
            $list   = CustomerVisit::where('AddTime', '>', $date)->where('AddTime', '<=', $enddate . ' 24:00')->group('CustomerId')->field('BussinessId')->select();
            if (mb_substr($date, 5, 2) !== mb_substr($enddate, 5, 2)) {
                $rdata['timestr'] = mb_substr($date, 5, 5) . '~' . mb_substr($enddate, 6, 5);
            } else {
                $rdata['timestr'] = mb_substr($date, 8, 2) . '~' . mb_substr($enddate, 8, 2);
            }
        }
        $userlist = [];
        $uid = [];
        foreach ($list as $key => $value) {
            if(empty($userlist[$value['BussinessId']])){
                $userlist[$value['BussinessId']]['Count'] = 1;
                $uid[] = $value['BussinessId'];
            }else{
                $userlist[$value['BussinessId']]['Count'] ++;
            }
        }
        // $userdata = User::where('UserId','in',$uid)->column('Name','UserId');
        // $rdata['list'] = [];

        // foreach ($userdata as $key => $value) {
        //     // $userlist[$key]['Name'] = $value;
        //     $rdata['list'][] = ['Name'=>$value,'Count'=>$userlist[$key]['Count'],'BussinessId'=>$key];
        // }

        return $userlist;
    }

}
